<template>
  <div class="app-container">
    <div class="filter-container">

      <el-select
        v-model="listQuery.channelId"
        filterable
        clearable
        :filter-method="dataFilter"
        placeholder="选择渠道"
        style="width: 150px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      >
        <el-option v-for="item in options" :key="item.channelId" :label="item.channel_name" :value="item.channelId" />
      </el-select>
      <el-date-picker
        v-model="times"
        class="filter-item"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="width: 250px;"
        type="daterange"
        value-format="yyyy-MM-dd"
      />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-download" @click="handleExport">
        导出
      </el-button>
      <p>
        <span>总注册人数：{{ userAll }} </span>
        <span style="padding-left:20px;">总用户唯一访问量：{{ uniqueAll }}</span>
        <span style="padding-left:20px;">平均注册点击比：{{ signClickRateAllFormat }}</span>
      </p>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="编号" prop="id" align="center">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="渠道名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="访问量" align="center">
        <template slot-scope="{row}">
          <span>{{ row.all === null ? '' : row.all }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户唯一访问量" align="center">
        <template slot-scope="{row}">
          <span>{{ row.unique === null ? '' : row.unique }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册人数" align="center">
        <template slot-scope="{row}">
          <span>{{ row.usercnt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册点击比" align="center">
        <template slot-scope="{row}">
          <span>{{ row.signClickRate }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="channelCount"
    />
  </div>
</template>

<script>
import { getChannelIdAndNick } from '@/api/user'
import { channelCount, exportReportChannel } from '@/api/sys'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'LoanCount',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      optionsCopy: [],
      options: [],
      limitNum: 1,
      fileList: [],
      comment: '',
      tableData: [],
      total: 0,
      userAll: 0,
      uniqueAll: 0,
      signClickRateAllFormat: 0,
      tableKey: 0,
      listQuery: {
        current: 1,
        size: 10,
        startTime: '',
        endTime: ''
      },
      times: [],
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '新增'
      },
      dialogPvVisible: false,
      pvData: [],
      downloadLoading: false,
      listLoading: false
    }
  },
  created() {
    this.channelCount(this.listQuery)
    this.getChannelIdAndNick()
  },
  methods: {
    handleExport() {
      exportReportChannel(this.listQuery).then(res => {
        if (res.data.code === 200) {
          window.location.href = res.data.data
        } else {
          this.$notify({
            title: '错误',
            message: '导出失败，请联系后台技术人员',
            type: 'fail',
            duration: 2000
          })
        }
      })
    },
    // 获取列表
    async channelCount() {
      if (this.times == null) {
        this.listQuery.endTime = ''
        this.listQuery.startTime = ''
      } else if (this.times.length === 0) {
        this.listQuery.endTime = ''
        this.listQuery.startTime = ''
      } else {
        this.listQuery.endTime = this.times[1]
        this.listQuery.startTime = this.times[0]
      }
      const res = await channelCount(this.listQuery)
      this.tableData = res.data.data.loanAppUvChannelReportIpage.records
      this.total = res.data.data.loanAppUvChannelReportIpage.total
      this.userAll = res.data.data.userAll
      this.uniqueAll = res.data.data.uniqueAll
      this.signClickRateAllFormat = res.data.data.signClickRateAllFormat
    },
    handleFilter() {
      this.listQuery.current = 1
      if (this.listQuery.startTime === null) {
        this.listQuery.startTime = ''
      }
      if (this.listQuery.endTime === null) {
        this.listQuery.endTime = ''
      }
      this.channelCount()
    },
    cancel() {
      this.dialogFormVisible = false
      this.channelCount()
    },
    resetTemp() {
      this.loan = {
        id: undefined,
        username: '',
        gmtDatetime: '',
        state: 0
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      // 刷新表单数据验证提示
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    dataFilter(val) {
      this.value = val
      if (val) { // val存在
        this.options = this.optionsCopy.filter((item) => {
          if (!!~item.channel_name.indexOf(val) || !!~item.channel_name.toUpperCase().indexOf(val.toUpperCase())) {
            return true
          }
        })
      } else { // val为空时，还原数组
        this.options = this.optionsCopy
      }
    },
    async getChannelIdAndNick() {
      const res = await getChannelIdAndNick(this.listQuery)
      res.data.data.forEach(element => {
        if (element.channel_name !== null) {
          this.optionsCopy.push(element)
          this.options.push(element)
        }
      })
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
  }
}
</script>

<style scoped>
.pic{
  width: 150px;
  margin-top: 10px;
  margin-left: 10px;
}
.ok{
  margin-left: 10px;
  margin-top: 10px;
  font-weight: 5 00;
  color:rgb(35, 248, 124)
}
.fail{
  margin-left: 10px;
  margin-top: 10px;
  font-weight: 5 00;
  color: rgb(240, 174, 76);
}
.info{
  margin-left: 10px;
  margin-top: 10px;
  font-weight: 5 00;
}
.info-name{
  margin-left: 50px;
  margin-top: 10px;
  color:rgb(172, 176, 177);
}
.title{
  margin-top: 10px;
  margin-left: 10px;
  font-size: 20px;
  font-weight: 400;
}
.box{
  margin-top: 10px;
  width: 100%;
  border: solid rgb(138, 219, 230) 2px;
}
.nick{
  font-size: 30px;
  font-weight: 600;
}
.link-top {
  margin-left: 10px;
  margin-top: 10px;
  width: 100%;
  height: 2px;
  border-top: solid rgb(138, 219, 230) 2px;
}
.link-title {
  margin-left: 10px;
  margin-top: 10px;
  width: 80px;
  height: 2px;
  border-top: solid rgb(172, 176, 177) 2px;
}
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
